package com.fita.admin.entity.search.order;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.fita.entity.order.OrderStatus;
import com.fita.entity.order.PayType;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;
import java.util.WeakHashMap;


/**
 * 
 * @author 宋剑宏
 *
 */
public class OrderSearch implements QueryBuilder{
	
	private Long orderId;
	
	private String code;
	
	  /**
     * alamo 系统订单号
     */
    private String alamoCode;
	
	/**
	 * 开始下单时间
	 * 
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date beginCreateTime;
	
	/**
	 * 结束下单时间
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date endCreateTime;
	
	/**
	 * 开始取车时间
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date beginPickupDate;
	
	/**
	 * 结束取车时间
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date endPickupDate;
	
	/**
	 * 开始还车时间
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date beginReturnDate;
	
	/**
	 * 结束还车时间
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date endReturnDate;
	/**
	 * 品牌
	 */
	private String brand;
	
	/**
	 * 状态
	 */
	private OrderStatus[] status;

	/**
	 * 国家
	 */
	private String country;
	
	
	/**
	 * 取车地点
	 */
	private String pickupLocation;
	
	/**
	 * 还车地点
	 */
	private String returnLocation;
	
	/**
	 * 预付/到付
	 */
	private PayType type;
	
	/**
	 * 车型
	 */
	private String vehicleType;
	
	/**
	 * 订单金额
	 */
	private String payPrice;
	
	/**
	 * 成本价
	 */
	private String costPrice;
	
	/**
	 * 代理人
	 */
	private String agentName;
	
	/**
	 * 代理返点
	 */
	private String rebate;
	
	
	/**
	 * 起始行
	 */
	private Integer beginIndex;
	
	/**
	 * 截止行
	 */
	private Integer endIndex;

	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date beginCancelTime;

	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date endCancelTime;

	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date beginRefundTime;

	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date endRefundTime;

	private QueryFilter filter;

	private QueryOrder order;
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
	 */
	public QueryFilter getPreparedFilter(boolean refresh) {
		if(filter == null || refresh) {
			filter = new QueryFilter().addEquals("country", country)
					.addEquals("brand", brand)
					.addEquals("code", code)
					.addDateTimeBetween("createTime", beginCreateTime, endCreateTime)
					.addDateTimeBetween("pickupDate", beginPickupDate, endPickupDate)
					.addDateTimeBetween("returnDate", beginReturnDate, endReturnDate)
					.addEquals("pickupLocation", pickupLocation)
					.addEquals("returnLocation", returnLocation)
					.addEquals("alamoCode", alamoCode)
					.addSentence(" status IN :status ", "status", status)
					.addEquals("type", type)
					.addEquals("vehicleType", vehicleType)
					.addEquals("payPrice", payPrice)
					.addEquals("costPrice", costPrice)
					.addEquals("agent.name", agentName)
					.addEquals("rebate", rebate)
					.addDateTimeBetween("cancelTime", beginCancelTime, endCancelTime)
					.addDateTimeBetween("refundTime", beginRefundTime, endRefundTime);
					//.addEquals("disabled", false);
		}
		return filter;
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
	 */
	public QueryOrder getOrder() {

		if (order == null){
			order = new QueryOrder();
		}
		order.add("id", "DESC");
		return order;
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
	 */
	public String buildSentenceTemplate() {
		
		String sentence = "FROM Order WHERE disabled = false AND status != 'CREATED' " + getPreparedFilter(false).clauseSentence()+ getOrder().sentence();
		
		//sentence = "SELECT * FROM (" + sentence + ") WHERE rn >= " + beginIndex;
		
		return sentence;
	}

	
	 public RowMapper<Map<String, Object>> buildRowMapper() {
	        
	        RowMapper<Map<String, Object>> rowMapper = new RowMapper<Map<String,Object>>() {
	            
	            /* (non-Javadoc)
	             * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
	             */
	            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
	                
	                Map<String, Object> result = new WeakHashMap<String, Object>();
	                result.put("code", rs.getObject("code"));
	                result.put("alamoCode", rs.getObject("alamoCode"));
	                result.put("createTime", rs.getObject("createTime"));
	                result.put("pickupDate", rs.getObject("pickupDate"));
	                result.put("returnDate", rs.getObject("returnDate"));
	                result.put("country", rs.getObject("country"));
	                result.put("brand", rs.getObject("brand"));
	                result.put("type", rs.getObject("type"));
	                result.put("payPrice", rs.getObject("payPrice"));
	                result.put("costPrice", rs.getObject("costPrice"));
	                result.put("currentExchangeRate", rs.getObject("currentExchangeRate"));
	                result.put("retailAmount", rs.getObject("retailAmount"));
	                result.put("agent.name", rs.getObject("agent.name"));
					result.put("status", rs.getObject("status"));
					result.put("refundTime", rs.getObject("refundTime"));
					return result;
	            }
	        };
	        return rowMapper;
	        
	    }
	
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
	 */
	public Map<String, Object> buildParameters() {
		return getPreparedFilter(false).parameters();
	}

	/**
	 * The default constructor
	 */
	public OrderSearch() {
		order = new QueryOrder();
	}
	
	/**
     * @return the status
     */
    public OrderStatus[] getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(OrderStatus[] status) {
        this.status = status;
    }

    /**
     * @param order the order to set
     */
    public void setOrder(QueryOrder order) {
        this.order = order;
    }

    /**
	 * @return the beginPickupDate
	 */
	public Date getBeginPickupDate() {
		return beginPickupDate;
	}

	/**
	 * @return the endPickupDate
	 */
	public Date getEndPickupDate() {
		return endPickupDate;
	}

	/**
	 * @return the beginReturnDate
	 */
	public Date getBeginReturnDate() {
		return beginReturnDate;
	}

	/**
	 * @return the endReturnDate
	 */
	public Date getEndReturnDate() {
		return endReturnDate;
	}

	/**
	 * @return the brand
	 */
	public String getBrand() {
		return brand;
	}

	/**
	 * @return the country
	 */
	public String getCountry() {
		return country;
	}

	/**
	 * @return the pickupLocation
	 */
	public String getPickupLocation() {
		return pickupLocation;
	}

	/**
	 * @return the returnLocation
	 */
	public String getReturnLocation() {
		return returnLocation;
	}

	/**
	 * @return the type
	 */
	public PayType getType() {
		return type;
	}

	/**
	 * @return the vehicleType
	 */
	public String getVehicleType() {
		return vehicleType;
	}

	/**
	 * @return the payPrice
	 */
	public String getPayPrice() {
		return payPrice;
	}

	/**
	 * @return the costPrice
	 */
	public String getCostPrice() {
		return costPrice;
	}

	/**
	 * @return the agentName
	 */
	public String getAgentName() {
		return agentName;
	}

	/**
	 * @return the rebate
	 */
	public String getRebate() {
		return rebate;
	}

	/**
	 * @return the filter
	 */
	public QueryFilter getFilter() {
		return filter;
	}

	/**
	 * @param beginPickupDate the beginPickupDate to set
	 */
	public void setBeginPickupDate(Date beginPickupDate) {
		this.beginPickupDate = beginPickupDate;
	}

	/**
	 * @param endPickupDate the endPickupDate to set
	 */
	public void setEndPickupDate(Date endPickupDate) {
		this.endPickupDate = endPickupDate;
	}

	/**
	 * @param beginReturnDate the beginReturnDate to set
	 */
	public void setBeginReturnDate(Date beginReturnDate) {
		this.beginReturnDate = beginReturnDate;
	}

	/**
	 * @param endReturnDate the endReturnDate to set
	 */
	public void setEndReturnDate(Date endReturnDate) {
		this.endReturnDate = endReturnDate;
	}

	/**
	 * @param brand the brand to set
	 */
	public void setBrand(String brand) {
		this.brand = brand;
	}

	/**
	 * @param country the country to set
	 */
	public void setCountry(String country) {
		this.country = country;
	}

	/**
	 * @param pickupLocation the pickupLocation to set
	 */
	public void setPickupLocation(String pickupLocation) {
		this.pickupLocation = pickupLocation;
	}

	/**
	 * @param returnLocation the returnLocation to set
	 */
	public void setReturnLocation(String returnLocation) {
		this.returnLocation = returnLocation;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(PayType type) {
		this.type = type;
	}

	/**
	 * @param vehicleType the vehicleType to set
	 */
	public void setVehicleType(String vehicleType) {
		this.vehicleType = vehicleType;
	}

	/**
	 * @param payPrice the payPrice to set
	 */
	public void setPayPrice(String payPrice) {
		this.payPrice = payPrice;
	}

	/**
	 * @param costPrice the costPrice to set
	 */
	public void setCostPrice(String costPrice) {
		this.costPrice = costPrice;
	}

	/**
	 * @param agentName the agentName to set
	 */
	public void setAgentName(String agentName) {
		this.agentName = agentName;
	}

	/**
	 * @param rebate the rebate to set
	 */
	public void setRebate(String rebate) {
		this.rebate = rebate;
	}

	/**
	 * @return the beginCreateTime
	 */
	public Date getBeginCreateTime() {
		return beginCreateTime;
	}

	/**
	 * @return the endCreateTime
	 */
	public Date getEndCreateTime() {
		return endCreateTime;
	}

	/**
	 * @param beginCreateTime the beginCreateTime to set
	 */
	public void setBeginCreateTime(Date beginCreateTime) {
		this.beginCreateTime = beginCreateTime;
	}

	/**
	 * @param endCreateTime the endCreateTime to set
	 */
	public void setEndCreateTime(Date endCreateTime) {
		this.endCreateTime = endCreateTime;
	}

	/**
	 * @return the alamoCode
	 */
	public String getAlamoCode() {
		return alamoCode;
	}

	/**
	 * @param alamoCode the alamoCode to set
	 */
	public void setAlamoCode(String alamoCode) {
		this.alamoCode = alamoCode;
	}

	/**
	 * @return the beginIndex
	 */
	public Integer getBeginIndex() {
		return beginIndex;
	}

	/**
	 * @return the endIndex
	 */
	public Integer getEndIndex() {
		return endIndex;
	}

	/**
	 * @param beginIndex the beginIndex to set
	 */
	public void setBeginIndex(Integer beginIndex) {
		this.beginIndex = beginIndex;
	}

	/**
	 * @param endIndex the endIndex to set
	 */
	public void setEndIndex(Integer endIndex) {
		this.endIndex = endIndex;
	}

	/**
	 * @return the orderId
	 */
	public Long getOrderId() {
		return orderId;
	}

	/**
	 * @param orderId the orderId to set
	 */
	public void setOrderId(Long orderId) {
		this.orderId = orderId;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	public Date getBeginCancelTime() {
		return beginCancelTime;
	}

	public void setBeginCancelTime(Date beginCancelTime) {
		this.beginCancelTime = beginCancelTime;
	}

	public Date getEndCancelTime() {
		return endCancelTime;
	}

	public void setEndCancelTime(Date endCancelTime) {
		this.endCancelTime = endCancelTime;
	}

	public Date getBeginRefundTime() {
		return beginRefundTime;
	}

	public void setBeginRefundTime(Date beginRefundTime) {
		this.beginRefundTime = beginRefundTime;
	}

	public Date getEndRefundTime() {
		return endRefundTime;
	}

	public void setEndRefundTime(Date endRefundTime) {
		this.endRefundTime = endRefundTime;
	}
}
